﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBenhVien
{
    public partial class frmQuanLyUser : Form
    {
        public frmQuanLyUser()
        {
            InitializeComponent();
        }
        private void combo_data()
        {
            DataTable lever = new DataTable();
            lever.Columns.Add("value", typeof(int));
            lever.Columns.Add("name", typeof(string));
            DataRow newrow = lever.NewRow();
            newrow["value"] = 0;
            newrow["name"] = "Phần quyền";
            lever.Rows.Add(newrow);
            newrow = lever.NewRow();
            newrow["value"] = 1;
            newrow["name"] = "Admin";
            lever.Rows.Add(newrow);
            newrow = lever.NewRow();
            newrow["value"] = 2;
            newrow["name"] = "Bác sĩ";
            lever.Rows.Add(newrow);
            newrow = lever.NewRow();
            newrow["value"] = 3;
            newrow["name"] = "Thuốc";
            lever.Rows.Add(newrow);
            newrow = lever.NewRow();
            newrow["value"] = 4;
            newrow["name"] = "Thu ngân";
            lever.Rows.Add(newrow);
            newrow = lever.NewRow();
            newrow["value"] = 5;
            newrow["name"] = "Tiếp tân";
            lever.Rows.Add(newrow);
            cb_quyen.DataSource = lever;
            cb_quyen.DisplayMember = "name";
            cb_quyen.ValueMember = "value";
        }
        private void gv_data(DataTable ds)
        {
            
            gview_user.DataSource = ds;
            gview_user.Columns[0].Visible = false;
            gview_user.Columns[1].HeaderText = "Tên người dùng";
            gview_user.Columns[2].Visible = false;
            gview_user.Columns[3].HeaderText = "Quyền hạn";
            gview_user.Columns[4].HeaderText = "Tên đầy đủ";
        }
        private void frmQuanLyUser_Load(object sender, EventArgs e)
        {
            combo_data();
            DataTable ds = DAOUser.getAllUser();
            gv_data(ds);
            cmd_xoa.Enabled = false;
        }

        private void gview_user_CellClick(object sender, DataGridViewCellEventArgs e)
        {
                int index = gview_user.CurrentCell.RowIndex;
                txt_tendn.Text = gview_user.Rows[index].Cells[1].Value.ToString();
                cb_quyen.SelectedValue = gview_user.Rows[index].Cells[3].Value;
                txt_tendaydu.Text = gview_user.Rows[index].Cells[4].Value.ToString();
                cmd_xoa.Enabled = true;
        }

        private void cmd_search_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txt_key.Text) && !string.IsNullOrWhiteSpace(txt_key.Text))
            {
                DataTable ds = DAOUser.getUserWithName(txt_key.Text);
                gv_data(ds);
            }
        }

        private void cmd_xoa_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Xác nhận xóa người dùng?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                int index = gview_user.CurrentCell.RowIndex;
                DAOUser.DelUser((int)gview_user.Rows[index].Cells[0].Value);
                DataTable ds = DAOUser.getAllUser();
                gv_data(ds);
            }
        }

        private void cmd_them_Click(object sender, EventArgs e)
        {
            DBOUser usr = new DBOUser();
            if (!string.IsNullOrEmpty(txt_tendn.Text))
            {
                if (!DAOUser.getUserWithUName(txt_tendn.Text))
                {
                    usr.username = txt_tendn.Text;
                }
                else
                {
                    MessageBox.Show("Tên đăng nhập này đã tồn tại.", "Thông Báo");
                    return;
                }
            }
            else
            {
                txt_tendn.Focus();
                return;
            }
            if (!string.IsNullOrEmpty(txt_matkhau.Text))
            {
                if (!string.IsNullOrEmpty(txt_nlmatkhau.Text))
                {
                    if (txt_matkhau.Text.Equals(txt_nlmatkhau.Text))
                    {
                        usr.password = txt_matkhau.Text;
                    }
                    else
                    {
                        MessageBox.Show("Hai mật khẩu không giống nhau.", "Thông Báo");
                        return;
                    }
                }
                else
                {
                    txt_nlmatkhau.Focus();
                    return;
                }
            }
            else
            {
                txt_matkhau.Focus();
                return;
            }
            if (!string.IsNullOrEmpty(txt_tendaydu.Text))
            {
                usr.fullname = txt_tendaydu.Text;
            }
            else
            {
                MessageBox.Show("Tên đầy đủ không được bỏ trống.", "Thông Báo");
                return;
            }
            if ((int)cb_quyen.SelectedValue > 0)
            {
                usr.usertype = (DBOUser.User_Type)cb_quyen.SelectedValue;
            }
            else
            {
                MessageBox.Show("Hãy chọn quyền hạn cho người dùng.", "Thông Báo");
                return;
            }
            DAOUser.AddUser(usr);
            DataTable ds = DAOUser.getAllUser();
            gv_data(ds);
            txt_tendn.Text = null;
            txt_matkhau.Text = null;
            txt_nlmatkhau.Text = null;
            txt_tendaydu.Text = null;
            cb_quyen.SelectedValue = 0;
        }

        private void cmd_capnhat_Click(object sender, EventArgs e)
        {
            int index = gview_user.CurrentCell.RowIndex;
            if (string.IsNullOrEmpty(txt_tendn.Text))
            {
                txt_tendn.Focus();
                return;
            }
            else
            {
                if (!txt_tendn.Text.Equals(gview_user.Rows[index].Cells[1].Value.ToString()))
                {
                    if (DAOUser.getUserWithUName(txt_tendn.Text))
                    {
                        MessageBox.Show("Người dùng này đã tồn tại.", "Thông Báo");
                        return;
                    }
                }
            }
            if (string.IsNullOrEmpty(txt_tendaydu.Text))
            {
                txt_tendaydu.Focus();
                return;
            }
            if (!string.IsNullOrEmpty(txt_matkhau.Text) && !string.IsNullOrEmpty(txt_nlmatkhau.Text))
            {
                {
                    if (txt_matkhau.Text.Equals(txt_nlmatkhau.Text))
                    {
                        DBOUser ur = new DBOUser();
                        ur.id = (int)gview_user.Rows[index].Cells[0].Value;
                        ur.username = txt_tendn.Text;
                        ur.password = txt_matkhau.Text;
                        ur.usertype = (DBOUser.User_Type)cb_quyen.SelectedValue;
                        ur.fullname = txt_tendaydu.Text;
                        DAOUser.UpdateFullUsr(ur);
                        DataTable ds = DAOUser.getAllUser();
                        gv_data(ds);
                    }
                }

            }
            else
            {
                DBOUser ur = new DBOUser();
                ur.id = (int)gview_user.Rows[index].Cells[0].Value;
                ur.username = txt_tendn.Text;
                ur.usertype = (DBOUser.User_Type)cb_quyen.SelectedValue;
                ur.fullname = txt_tendaydu.Text;
                DAOUser.UpdateNonPassUsr(ur);
                DataTable ds = DAOUser.getAllUser();
                gv_data(ds);
            }
        }

        private void gview_user_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.gview_user.Columns[e.ColumnIndex].Name == "usertype") 
            {
                switch ((int)e.Value)
                {
                    case 1:
                        e.Value = "Admin";
                        break;
                    case 2:
                        e.Value = "Bác Sĩ";
                        break;
                    case 3:
                        e.Value = "Thuốc";
                        break;
                    case 4:
                        e.Value = "Thu Ngân";
                        break;
                    case 5:
                        e.Value = "Tiếp Tân";
                        break;
                }
            }
        }
    }
}
